1660E - Matrix and Shifts - CodeForces Solution


constructive algorithms greedy implementation

Please click on ads to support us..

Python Code:

import sys
import random
from bisect import bisect_left as lb
from bisect import bisect_right as rb
from collections import deque
from queue import PriorityQueue as pq
from math import gcd
input_ = lambda: sys.stdin.readline().strip("\r\n")
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
lii = lambda : list(map(int, list(ip())))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab,bc,cd : ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x : print(x)
prinT = lambda x : print(x)
f = lambda : sys.stdout.flush()
inv =lambda x:pow(x,mod-2,mod)
dx = [0,0,1,-1]
dy = [1,-1,0,0]
mod = 10**9 + 7
mod1 = 998244353

for _ in range (ii()) :
    ip()
    n = ii()
    a = []
    for i in range (n) :
        a.append(list(ip()))

    b = [0 for i in range (n+2)]
    t = 0

    for i in range (n) :
        for j in range (n) :
            if (a[i][j] == '1') :
                t += 1
                if (i > j)  :
                    b[i-j] += 1
                else :
                    b[n - j + i] += 1
    mn = 0

    for i in b :
        mn = max(mn,i)

    print(t + n - 2*mn)

    
    
    

C++ Code:

#include<bits/stdc++.h>
     
using namespace std;
     
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, string> pss;
typedef vector <int> vi;
typedef vector<bool> vb ;
typedef vector<string> vs ;
typedef vector <vi> vvi;
typedef vector<pii> vpii;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
     
const ll INF = 1000000000+7 ;
const ll MOD = 998244353 ;
     
#define all(X) (X).begin(), (X).end()
#define allr(X) (X).rbegin(), (X).rend()
#define sz(X) (int)X.size()
#define setbits(X)(long long) __builtin_popcountll(X)
#define fastio() ios_base::sync_with_stdio(false); cin.tie(NULL);
 
#define fi first
#define se second
#define pb push_back
#define maxxbysec [](const auto& lhs, const auto& rhs) { return lhs.second < rhs.second; }
    
bool sortbysec(const pair<auto,auto> &a,
               const pair<auto,auto> &b){   
    return (a.second < b.second);}   
 
void solve();

vector<bool> is_prime(1e7+1, true);

void sieve(){
   
   is_prime[0] = is_prime[1] = false;
    for (ll i = 2; i <= 1e7 ; i++) {
        if (is_prime[i] && 1ll*i * i <= 1e7) {
            for (ll j = 1ll*i * i; j <= 1e7; j += i)
                is_prime[j] = false;
        }
    }}
ll binpow(ll a, ll b , ll mod = INF) {
 
     if( b == 0 )
         return 1 ;
     
     ll res = 1  , x = a ;
     for( ll i = 0 ; i < 63 ; ++i ){
         if( (1ll<<i) & b )
             res = (res*x) % mod ;
         x = (x*x) % mod ;
     }
     return res ;
 }
ll fact( ll n , ll mod = INF){
    ll res = 1 ;
    for( ll i = 1 ; i <= n ; ++i )
        res = (res*i)%mod ;
    return res ; } 
vll factorization(ll n) {
    vector<long long> factorisation;
    for (long long d = 2; d * d <= n; d++) {
        while (n % d == 0) {
            factorisation.push_back(d);
            n /= d;
        }
    }
    if (n > 1)
        factorisation.push_back(n);
    return factorisation;}
class DisjointSet{
public: 

    vector<ll> parent;
    vector<ll> size;

    void make_set(ll n){
        
        parent.resize(n, 0);
        size.resize(n, 1);
        for(ll i = 0 ; i < n ; i++)
            parent[i]= i;
        
    }

    ll find_set(ll v) {
        if (v == parent[v])      // Path Compression
            return v;
        return parent[v] = find_set(parent[v]);
    }

    void union_sets(ll u, ll v) {
        v = find_set(v);
        u = find_set(u);
        if (v != u) {
            if (size[v] < size[u])
                swap(v, u);
            parent[u] = v;
            size[v] += size[u];
        }
    }

    ll len( ll u ){
        return size[find_set(u)] ;
    }};
    
int main()
{   
    fastio();
    
    long long t=1;
    cin>>t;

    while(t--){
        solve();
    }
    
    return 0;
} 



 
void solve(){  
 
    ll n ;
    cin >> n ;

    vs v(n) ;
    ll ones = 0 ;
    for( ll i =0 ; i < n ; ++i ){
        cin >> v[i] ;
        ones += count( all(v[i]) , '1' ) ;
    }

    ll maxx = 0 ;
    for( ll i = 0 ; i < n ; ++i ){
        ll r = 0 , c = i , lres = 0 ;
        do{
            lres += (v[r][c]-'0') ;
            r = (r+1)%n ;
            c = (c+1)%n ;
            }while( r != 0 ) ;
        maxx = max( maxx , lres ) ;
    }

    cout << ones-maxx + (n-maxx)<<'\n' ;

}


Comments

Submit
0 Comments
More Questions

442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array
198. House Robber
153. Find Minimum in Rotated Sorted Array
150. Evaluate Reverse Polish Notation
144. Binary Tree Preorder Traversal
137. Single Number II
130. Surrounded Regions
129. Sum Root to Leaf Numbers
120. Triangle
102. Binary Tree Level Order Traversal